package com.wwp.wkb.base.impl;

import android.app.Activity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wwp.wkb.R;
import com.wwp.wkb.base.Loading;
import com.wwp.wkb.base.Request;

public class LoadingSwipeRefresh implements Loading {
    private Request request;
    private LoadingView loadingView;
    private SwipeRefreshLayout swipeRefreshLayout;

    public LoadingSwipeRefresh(View view, int loadingId, int refreshId) {
        if (loadingId != 0) {
            loadingView = (LoadingView) view.findViewById(loadingId);
        }
        if (refreshId != 0) {
            swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(loadingId);
            swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    if (request != null) {
                        request.request();
                    }
                }
            });
        }
    }

    public LoadingSwipeRefresh(Activity activity, int loadingId, int refreshId) {
        if (loadingId != 0) {
            loadingView = (LoadingView) activity.findViewById(loadingId);
        }
        if (refreshId != 0) {
            swipeRefreshLayout = (SwipeRefreshLayout) activity.findViewById(loadingId);
            swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    if (request != null) {
                        request.request();
                    }
                }
            });
        }
    }

    @Override
    public void setRequest(Request request) {
        this.request = request;
    }

    @Override
    public void onLoading() {
        if (loadingView != null) {
            loadingView.showLoading();
        }
    }

    @Override
    public void onSucceed() {
        if (loadingView != null) {
            loadingView.showContent();
        }
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(false);
        }
    }

    @Override
    public void onFailure(String errCode, String errMsg) {
        onError(R.layout.temp_loading_error_view, R.mipmap.default_error, errMsg);
    }

    @Override
    public void onEmpty(String errMsg) {
        onError(R.layout.temp_loading_error_view, R.mipmap.default_error, errMsg);
    }

    @Override
    public void onError(int layoutId, int imgId, String errMsg) {
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(false);
        }
        if (loadingView != null) {
            View emptyView = loadingView.showView(layoutId);
            if (imgId != 0) {
                ImageView img = (ImageView) emptyView.findViewById(R.id.iv_icon);
                img.setImageResource(imgId);
            }
            if (!TextUtils.isEmpty(errMsg)) {
                TextView errorText = (TextView) emptyView.findViewById(R.id.errorText);
                errorText.setText(errMsg);
            }
            emptyView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (request != null) {
                        request.load();
                    }
                }
            });
        }
    }
}
